package com.lsw.jgyf.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.lsw.Base.BaseHolder;
import com.lsw.Base.BaseSimpleAdapter;
import com.lsw.jgyf.R;
import com.lsw.jgyf.model.FujiaxianModel;

/**
 * Created by Administrator on 2018/7/20 0020.
 */

public class FujiaxianAdapter extends BaseSimpleAdapter<FujiaxianModel> {
    public FujiaxianAdapter(Context context) {
        super(context);
    }

    @Override
    protected BaseHolder<FujiaxianModel> getHolder() {
        return new BaseHolder<FujiaxianModel>() {
            TextView tv_title;
            CheckBox checkBox;

            @Override
            public void bindViews(View parentView) {
                tv_title = parentView.findViewById(R.id.tv_title);
                checkBox = parentView.findViewById(R.id.checkBox);
            }

            @Override
            public void bindData(FujiaxianModel item, int position) {
                tv_title.setText(item.getName());
                if (item.getIs_must() == 1) {
                    checkBox.setChecked(true);
                    item.setIsseclect(true);
                }
                checkBox.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (item.getIs_must() == 1) {
                            return true;
                        }
                        return false;
                    }
                });
                tv_title.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.getIs_must() == 1) {
                            return;
                        }
                        checkBox.setChecked(!item.isIsseclect());
                    }
                });
                checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                    item.setIsseclect(isChecked);
                    if (adapterRefresh != null)
                        adapterRefresh.onRefreshAdapter(position, 1);
                });
            }
        };
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.item_fujiaxian;
    }
}
